package com.integral.xr.module.platform.controller.admin.pointsrule;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.integral.xr.framework.common.pojo.PageParam;
import com.integral.xr.framework.common.pojo.PageResult;
import com.integral.xr.framework.common.pojo.CommonResult;
import com.integral.xr.framework.common.util.object.BeanUtils;
import static com.integral.xr.framework.common.pojo.CommonResult.success;

import com.integral.xr.framework.excel.core.util.ExcelUtils;

import com.integral.xr.framework.operatelog.core.annotations.OperateLog;
import static com.integral.xr.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.integral.xr.module.platform.controller.admin.pointsrule.vo.*;
import com.integral.xr.module.platform.dal.dataobject.pointsrule.PointsRuleDO;
import com.integral.xr.module.platform.service.pointsrule.PointsRuleService;

@Tag(name = "管理后台 - 积分规则")
@RestController
@RequestMapping("/platform/points-rule")
@Validated
public class PointsRuleController {

    @Resource
    private PointsRuleService pointsRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建积分规则")
    @PreAuthorize("@ss.hasPermission('platform:points-rule:create')")
    public CommonResult<String> createPointsRule(@Valid @RequestBody PointsRuleSaveReqVO createReqVO) {
        return success(pointsRuleService.createPointsRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分规则")
    @PreAuthorize("@ss.hasPermission('platform:points-rule:update')")
    public CommonResult<Boolean> updatePointsRule(@Valid @RequestBody PointsRuleSaveReqVO updateReqVO) {
        pointsRuleService.updatePointsRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('platform:points-rule:delete')")
    public CommonResult<Boolean> deletePointsRule(@RequestParam("id") String id) {
        pointsRuleService.deletePointsRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('platform:points-rule:query')")
    public CommonResult<PointsRuleRespVO> getPointsRule(@RequestParam("id") String id) {
        PointsRuleDO pointsRule = pointsRuleService.getPointsRule(id);
        return success(BeanUtils.toBean(pointsRule, PointsRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分规则分页")
    @PreAuthorize("@ss.hasPermission('platform:points-rule:query')")
    public CommonResult<PageResult<PointsRuleRespVO>> getPointsRulePage(@Valid PointsRulePageReqVO pageReqVO) {
        PageResult<PointsRuleDO> pageResult = pointsRuleService.getPointsRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PointsRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分规则 Excel")
    @PreAuthorize("@ss.hasPermission('platform:points-rule:export')")
    @OperateLog(type = EXPORT)
    public void exportPointsRuleExcel(@Valid PointsRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PointsRuleDO> list = pointsRuleService.getPointsRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "积分规则.xls", "数据", PointsRuleRespVO.class,
                        BeanUtils.toBean(list, PointsRuleRespVO.class));
    }

}